/*
 * Copyright (c) 2011 Salzburg Research.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package at.newmedialab.skwrl.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * Reason Maintenance: A justification for the existence of a triple. The justification
 * contains a reference to the triple it supports and references to the triples and
 * rules that support this justification.
 * <p/>
 * A justification is created and stored each time a reasoning rule infers new triples.
 * Each of the inferred triples will be supported by the rules and base triples that
 * were used in the inference. This makes both explanations and maintenance much simpler,
 * i.e. when a base triple is deleted, we can remove all justifications that contain it
 * and then remove all inferred triples that are no longer supported by any justification.
 * <p/>
 * The generic argument Triple is the implementation-dependent representation of a triple.
 * It is assumed that the representation properly implements the .equals() and .hashCode()
 * methods.
 * <p/>
 * User: sschaffe
 */
public class Justification<Triple> implements Serializable {

    private Long id;

    /**
     * The triple that is supported by this justification
     */
    private Triple triple;

    /**
     * The base triples that support this justification
     */
    private Set<Triple> supportingTriples;


    /**
     * The rules that support this justification
     */
    private Set<Rule> supportingRules;

    /**
     * The version of the justification (for optimistic locking).
     */
    private Long version;


    public Justification() {
        supportingTriples = new HashSet<Triple>();
        supportingRules   = new HashSet<Rule>();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Triple getTriple() {
        return triple;
    }

    public void setTriple(Triple triple) {
        this.triple = triple;
    }

    public Set<Triple> getSupportingTriples() {
        return supportingTriples;
    }

    public void setSupportingTriples(Set<Triple> supportingTriples) {
        this.supportingTriples = supportingTriples;
    }

    public Set<Rule> getSupportingRules() {
        return supportingRules;
    }

    public void setSupportingRules(Set<Rule> supportingRules) {
        this.supportingRules = supportingRules;
    }

    public Long getVersion() {
        return version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Justification that = (Justification) o;

        //if (id != null ? !id.equals(that.id) : that.id != null) return false;
        if (!supportingRules.equals(that.supportingRules)) return false;
        if (!supportingTriples.equals(that.supportingTriples)) return false;
        if (!triple.equals(that.triple)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = 0; // id != null ? id.hashCode() : 0;
        result = 31 * result + triple.hashCode();
        result = 31 * result + supportingTriples.hashCode();
        result = 31 * result + supportingRules.hashCode();
        return result;
    }
}
